/**
 * 这是一个逻辑类（对方）
 * 控制游戏和绑定各种键盘事件
 */
import {Game} from './game.js';

class Remote{
	constructor(socket){
		this.dom = {
			gameBox: document.querySelector('#remote-game-box'),
			nextBox: document.querySelector('#remote-next-box'),
			timeBox: document.querySelector('#remote-time'),
			scoreBox: document.querySelector('#remote-score'),
			statusBox: document.querySelector('#remote-status')
		};
		this.game = new Game();
		this.socket = socket;
	}

	/**
	 * 游戏开始的方法
	 * @param  {Number} type 方块种类 
	 * @param  {Number} dir  旋转类型
	 * @return {[type]}       [description]
	 */
	start(type, dir){
		this.game.init(this.dom, type, dir);
		this.bingKeyEvent();
	}

	bingKeyEvent(){
		let that = this;

		this.socket.on('next', function(data){
			that.game.nextPerform(data.type, data.dir);
		});

		this.socket.on('rotate', function(data){
			that.game.rotate();
		});

		this.socket.on('down', function(data){
			that.game.down();
		});

		this.socket.on('left', function(data){
			that.game.left();
		});

		this.socket.on('right', function(data){
			that.game.right();
		});

		this.socket.on('fall', function(data){
			that.game.fall();
		});

		this.socket.on('fixed', function(data){
			that.game.fixed();
		});

		this.socket.on('time', function(data){
			that.game.setTime(data);
		});

		this.socket.on('lost', function(data){
			that.game.result(data);
		});

		this.socket.on('line', function(data){
			that.game.checkClear();
			that.game.setScore(data);
		});

		this.socket.on('addLines', function(data){
			that.game.addLines(data);
		});
	}
}

export {Remote};